package com.jlkf.service.impl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.jlkf.dao.TblCaptchaMapper;
import com.jlkf.dao.TblIntegralSetMapper;
import com.jlkf.dao.TblMessageMapper;
import com.jlkf.dao.TblOrderMapper;
import com.jlkf.dao.TblRelationMapper;
import com.jlkf.dao.TblSortMapper;
import com.jlkf.dao.TblUserAuthorizeMapper;
import com.jlkf.dao.TblUserMapper;
import com.jlkf.dao.TblUserPurseInfoMapper;
import com.jlkf.dao.TblVipInfoMapper;
import com.jlkf.pojo.TblCaptcha;
import com.jlkf.pojo.TblIntegralSet;
import com.jlkf.pojo.TblMessage;
import com.jlkf.pojo.TblOrder;
import com.jlkf.pojo.TblRelation;
import com.jlkf.pojo.TblSort;
import com.jlkf.pojo.TblUser;
import com.jlkf.pojo.TblUserAuthorize;
import com.jlkf.pojo.TblUserPurseInfo;
import com.jlkf.pojo.TblVipInfo;
import com.jlkf.service.UserService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.AliyunSms;
import com.platform.server.util.AuroraPush;
import com.platform.server.util.HttpRequestUtil;
import com.platform.server.util.MD5Util;
import com.platform.server.util.RSAUtil;
import com.platform.server.util.RegexUtil;
import com.platform.server.util.StringTools;
import com.platform.server.util.UUIDUtil;
import com.platform.server.util.WeixinCons;

@Service
public class UserServiceImpl implements UserService{

	 // 用户信息
	@Autowired
	private TblUserMapper tblUserMapper;
	
	// 验证码信息
	@Autowired
	private TblCaptchaMapper tblCaptchaMapper;
	
	// 第三方信息
	@Autowired
	private TblUserAuthorizeMapper tblUserAuthorizeMapper;
	
	// 分类消息
	@Autowired
	private TblSortMapper tblSortMapper;
	
	@Autowired
	private TblOrderMapper tblOrderMapper;
	
	@Autowired
	private TblVipInfoMapper tblVipInfoMapper;
	
	@Autowired
	private TblUserPurseInfoMapper tblUserPurseInfoMapper;
	
	@Autowired
	private TblIntegralSetMapper tblIntegralSetMapper;
	
	@Autowired
	private TblMessageMapper tblMessageMapper;
	
	@Autowired
	private TblRelationMapper tblRelationMapper;
	
	
	@Override
	public ReturnData loginUser(String json) {
		 TblUser json2Obj = StringTools.json2Obj(json,TblUser.class);
		 if(json2Obj!=null){
			 String getgUserPassword = json2Obj.getgUserPassword();
			 if (!RegexUtil.isMobile(json2Obj.getgUserPhone())) {
		          return new ReturnData(201, "无效手机号码");
		        }else if (getgUserPassword.length() < 6 ) {
				  return new ReturnData(201, "密码长度不少于6位");
				}
			     // 获取密码 转成 base64和MD5
			    String md5byBase64 = MD5Util.getMD5byBase64(getgUserPassword);
			    json2Obj.setgUserPassword(md5byBase64);
			    Map<String, Object> map = new HashMap<String,Object>();
			    map.put("gUserPhone", json2Obj.getgUserPhone());
			    TblUser loginUser = tblUserMapper.loginUser(map);
			     if(loginUser==null){
			    	 return new ReturnData(203,"账号不存在!");
			     }else{
			    	 map.put("gUserPassword", json2Obj.getgUserPassword());
			    	 TblUser user = tblUserMapper.loginUser(map);
			    	 if(user==null){
			    		 return new ReturnData(203,"密码错误!");
			    	 }else{
			    		 if(user.getgStatus()==2){
				    		 return new ReturnData(203,"您的账号已被封号，请联系客服!");
				    	 }
				    	 tblUserMapper.updateLoginTime(user.getgId());
				    	 loginUser.setgUserPassword(null);
				    	 List<TblUserAuthorize> list = user.getAuthorizes();
						  for(TblUserAuthorize authorize:list){
							  if(authorize.getgType()==1){
								  user.setWeixinName(authorize.getgAuthorizeName());
							  }else if(authorize.getgType()==2){
								  user.setWeiboName(authorize.getgAuthorizeName());
							  }else if(authorize.getgType()==3){
								  user.setQqName(authorize.getgAuthorizeName());
							  } 
						  }
				    	 return new ReturnData(user,200,"登录成功");
			    	 } 
			     }
		  }
		return new ReturnData(100, "参数列表为空");
	}

	/**
	 *  发送短信验证码
	 */
	@Override
	public ReturnData sendCode(String phone, Integer type) {
		 try{
			if (!RegexUtil.isMobile(phone)) { 
		    	return new ReturnData(500,"无效手机号");
			}
			Map<String, Object> map1 = new HashMap<>();
			map1.put("phone", phone);
			if(type==1){
			    TblUser selectPhone = tblUserMapper.getPhoneUserInfo(map1);
			    if(selectPhone!=null){
				   return new ReturnData(1001, "当前手机已被注册");
			    }
			}else if (type==3 || type==2){
				TblUser phoneUserInfo = tblUserMapper.getPhoneUserInfo(map1);
	    		if(phoneUserInfo==null){
	    		   return new ReturnData(1200, "当前手机未注册"); 
	    		}
			}
		    
			// 发送验证码
			String code = UUIDUtil.getFixLenthInt(4);
			String SignName = "狮兔在线";//模板名称
			String template ="SMS_127120046";//模板id
	
		    SendSmsResponse result = AliyunSms.sendSms(SignName, phone, code, template);
		    if(result==null){ 
		    	return new ReturnData(500,"请求过于频繁");
		    }else if (result.getCode().equals("OK")) {
			   TblCaptcha authCode = new TblCaptcha();
			   authCode.setCreateTime(new Date());
			   authCode.setGenerateTime(new Date());
			   //验证码5分钟过期
			   Calendar nowTime = Calendar.getInstance();
			   nowTime.add(Calendar.MINUTE, 5);
			   authCode.setExpireTime(nowTime.getTime());
			   authCode.setPhone(phone);
			   authCode.setCode(code); 
			   authCode.setType(type.toString());
			   
			   int result1 =0; 
			   Map<String, Object> map = new HashMap<String,Object>();
			   map.put("type", type);
			   map.put("phone", phone);
			   TblCaptcha captcha = tblCaptchaMapper.getCode(map);
			   if(captcha != null) { 
				   result1 = tblCaptchaMapper.deleteByPrimaryKey(captcha.getId());
			   } 
			   result1 = tblCaptchaMapper.insertSelective(authCode); 
			   if(result1 > 0) {
			       return new ReturnData(code,200,"发送验证码成功");
			   }else{
				   return new ReturnData(200,"发送验证码失败");
			   }
		    }else{
		    	 return new ReturnData(200,"发送验证码失败");
		    }
		 }catch (Exception e) {
			 e.printStackTrace(); 
			 return new ReturnData(500,"系统错误");
	     }    
	}
	
	/**
	 * 注册用户
	 */
	@Override
	public ReturnData regisUser(String json) {
		 TblUser json2Obj = StringTools.json2Obj(json, TblUser.class);
		    if(json2Obj!=null){
		    	
		    	 if (!RegexUtil.isMobile(json2Obj.getgUserPhone())) {
			          return new ReturnData(201, "无效手机号码");
			        }
		    	 if (json2Obj.getgUserPassword().length() < 6 ) {
					  return new ReturnData(201, "密码长度不少于6位");
					}
		    	 
		    	String code = json2Obj.getCode();
		    	Map<String, Object> map = new HashMap<>();
		    	// 1代表注册
		    	map.put("type", 1);
		    	map.put("phone", json2Obj.getgUserPhone());
		    	// 获取验证码 对象
		    	TblCaptcha code2 = tblCaptchaMapper.getCode(map);
		        // 获取系统当前时间戳
		    	long currentTimeMillis = System.currentTimeMillis();
		    	//  验证码结束时间内
		    	Date expireTime = code2.getExpireTime();
		    	if(code2!=null){
		    		if(currentTimeMillis-expireTime.getTime()>0){
			    		return new ReturnData(500, "验证码已过期!");
			    	}else if(!code.equals(code2.getCode())){
			    		return new ReturnData(500, "验证码不正确!");
			    	}else{
			    		TblUser selectPhone = tblUserMapper.getPhoneUserInfo(map);
			    		 if(selectPhone!=null){
			    			  return new ReturnData(1001, "当前手机已被注册");
			    		 }
			    		// base64加密
			    		String md5byBase64 = MD5Util.getMD5byBase64(json2Obj.getgUserPassword());
			    		json2Obj.setgUserPassword(md5byBase64);
			    		json2Obj.setgUserCreateTime(new Date());
		        		json2Obj.setgUserUpdateTime(new Date());
		        		json2Obj.setgUserNo(UUIDUtil.getFixLenthInt(8));
		        		if(StringTools.isEmpty(json2Obj.getgUserAvatar())){
		        			json2Obj.setgUserAvatar("http://120.79.157.154:8899/hqsmFile/HQSMIMG/head_default.png");
		        		}
			    		int i = tblUserMapper.insertSelective(json2Obj);
			    		if(i==1){
			    			//分享注册得积分
			    			if(json2Obj.getShareId()!=null){
			    				TblIntegralSet integralSet = tblIntegralSetMapper.selectByType(6);
			    				TblUser selectByPrimaryKey = tblUserMapper.selectByPrimaryKey((long)json2Obj.getShareId());
			    				TblUser record2 = new TblUser();
			    				record2.setgUserIntegral(selectByPrimaryKey.getgUserIntegral()+integralSet.getIntegral());
			    				record2.setgId((long)json2Obj.getShareId());
			    				record2.setgUserUpdateTime(new Date());
			    				int u= tblUserMapper.updateByPrimaryKeySelective(record2);	
			    				//新增积分明细
			    				TblUserPurseInfo purseInfo = new TblUserPurseInfo();
			    				purseInfo.setgContent("分享链接注册赠送积分");
			    				purseInfo.setgCreatetime(new Date());
			    				purseInfo.setgStatus(1);
			    				purseInfo.setgTitle("分享链接注册");
			    				purseInfo.setgType(2);
			    				purseInfo.setgBalance(selectByPrimaryKey.getgUserIntegral()+integralSet.getIntegral());
			    				purseInfo.setgUserid((long)json2Obj.getShareId());
			    				purseInfo.setgMoney((double)integralSet.getIntegral());
			    				tblUserPurseInfoMapper.insertSelective(purseInfo);
			    				
			    				int i1 = new Double(purseInfo.getgBalance()).intValue();
			    				TblMessage record3 = new TblMessage();
			        			record3.setgContent("分享链接注册获得"+integralSet.getIntegral()+"积分,现有积分"+i1);
			        			record3.setgTitle("积分提示");
			        			record3.setgType(2);
			        			record3.setgUserId((long)json2Obj.getShareId());
			        			record3.setgCreatetime(new Date());
			        			record3.setgVersion(1);
			        			tblMessageMapper.insertSelective(record3);
			    			}
			    			
			    			//建立上下级关系
			    			if(json2Obj.getSuperiorId()!=null){
			    				createRelaiton(json2Obj.getSuperiorId(), json2Obj.getgId().intValue());
			    			}
			    			return new ReturnData(json2Obj,200, "注册成功");
			    		}
			    	}
		    	}
		    }
		return new ReturnData(100, "参数列表为空");
	}

	/**
	 * 验证码登录
	 */
	@Override
	public ReturnData codeLogin(String phone, String code) {
           if(StringTools.isEmpty(phone)){
        	   return new ReturnData(1001, "手机号码不能为空");
           }
       	Map<String, Object> map = new HashMap<>();
    	// 3代表 验证码登录
    	map.put("type", 3);
    	map.put("phone", phone);
    	// 获取验证码 对象
    	TblCaptcha code2 = tblCaptchaMapper.getCode(map);
          if(code2!=null){
        	  // 获取系统当前时间戳
          	long currentTimeMillis = System.currentTimeMillis();
          	//  验证码结束时间内
          	Date expireTime = code2.getExpireTime();
          	if(currentTimeMillis-expireTime.getTime()>0){
	    		return new ReturnData(500, "验证码已过期!");
	    	}else if(!code.equals(code2.getCode())){
	    		return new ReturnData(500, "验证码不正确!");
	    	}else{
	    		 // 通过 手机号码查询 用户信息
	    		 TblUser phoneUserInfo = tblUserMapper.getPhoneUserInfo(map);
	    		 if(phoneUserInfo==null){
	    		   return new ReturnData(1200, "登录失败,请先注册此手机号码。"); 
	    		 }else{
	    			 if(phoneUserInfo.getgStatus()==2){
			    		 return new ReturnData(203,"您的账号已被封号，请联系客服!");
			    	 }
			    	 tblUserMapper.updateLoginTime(phoneUserInfo.getgId());
	    			 phoneUserInfo.setgUserPassword(null);
	    		  return new ReturnData(phoneUserInfo,200, "登录成功"); 
	    		 }
	    	  }
          }   
		return new ReturnData(201, "请先发送验证码");
	}

	@Override
	public ReturnData forgetPassWord(String json) {
	
		 @SuppressWarnings("unchecked")
		Map<String, Object> jsonToMap = StringTools.jsonToMap(json);
		 
		 String phone = jsonToMap.get("phone").toString().trim();
		 
		 String code = jsonToMap.get("code").toString().trim();
		 
		 String passWord = jsonToMap.get("passWord").toString().trim();
		 
		 String confirmPassWord = jsonToMap.get("confirmPassWord").toString().trim();	
		 
		   Map<String, Object> map = new HashMap<>();
	    	// 3代表 修改密码 忘记密码
	    	map.put("type", 2);
	    	map.put("phone", phone);
	    	// 获取验证码 对象
	    	TblCaptcha code2 = tblCaptchaMapper.getCode(map);
			   if(code2!=null){
				  	long currentTimeMillis = System.currentTimeMillis();
		          	//  验证码结束时间内
		          	Date expireTime = code2.getExpireTime();				   
		          	if(currentTimeMillis-expireTime.getTime()>0){
			    		return new ReturnData(500, "验证码已过期!");
			    	}else if(!code.equals(code2.getCode())){
			    		return new ReturnData(500, "验证码不正确!");
			    	}else{
			    		 if(passWord.equals(confirmPassWord)){
			    			 String md5byBase64 = MD5Util.getMD5byBase64(passWord);
			    			 TblUser record = new TblUser();
			    			 record.setgUserPhone(phone);
			    			 record.setgUserUpdateTime(new Date());
			    			 record.setgUserPassword(md5byBase64);
							 int u = tblUserMapper.updatePassWord(record);
							 if(u==1){
								 return new ReturnData(200, "修改密码成功");	 
							 }else{
								 return new ReturnData(100, "修改密码失败,此账号不存在!");
							 }
			    		 }else{
			    			 return new ReturnData(1001, "俩次密码不一致!");
			    		 }
			    	}
			   }
		return new ReturnData(100,"请先发送验证码");
	}

	@Override
	public ReturnData authorizedLogin(String json) {
		TblUser json2Obj = StringTools.json2Obj(json, TblUser.class);
		  if(json2Obj!=null){
			  if(StringTools.isEmpty(json2Obj.getgUserPhone())){
				  return new ReturnData(1001,"请填写手机号码!");
			  }
			  /*if(StringTools.isEmpty(json2Obj.getgUserPassword())){
				  return new ReturnData(1002,"请填写密码!");
			  }*/
			  if(StringTools.isEmpty(json2Obj.getCode())){
				  return new ReturnData(1002,"请填写验证码!");
			  }
				Map<String, Object> map = new HashMap<>();
		    	// 3代表 验证码登录
		    	map.put("type", 4);
		    	map.put("phone", json2Obj.getgUserPhone());
		    	// 获取验证码 对象
		    	TblCaptcha code2 = tblCaptchaMapper.getCode(map);
		    	if(code2!=null){
		    		long currentTimeMillis = System.currentTimeMillis();
		          	//  验证码结束时间内
		          	Date expireTime = code2.getExpireTime();
		          	if(currentTimeMillis-expireTime.getTime()>0){
			    		return new ReturnData(500, "验证码已过期!");
			    	}else if(!json2Obj.getCode().equals(code2.getCode())){
			    		return new ReturnData(500, "验证码不正确!");
			        }else{
			        	 TblUser phoneUserInfo = tblUserMapper.getPhoneUserInfo(map);
			        	  if(phoneUserInfo==null){
			        		  if(json2Obj.getgUserPassword()!=null){
			        			  String getgUserPassword = json2Obj.getgUserPassword();
				        		  String md5byBase64 = MD5Util.getMD5byBase64(getgUserPassword);
				        		  json2Obj.setgUserPassword(md5byBase64);
			        		  }
			        		  json2Obj.setgUserCreateTime(new Date());
			        		  json2Obj.setgUserUpdateTime(new Date());
			        		  json2Obj.setgUserNo(UUIDUtil.getFixLenthInt(8));
			        		  json2Obj.setgUserName(json2Obj.getgAuthorizeName());
			        		  if(StringTools.isEmpty(json2Obj.getgUserAvatar())){
			        			  json2Obj.setgUserAvatar("http://120.79.157.154:8899/hqsmFile/HQSMIMG/head_default.png");
			        		  }
			        		  int i = tblUserMapper.insertSelective(json2Obj);
				        		  if(i==1){
				        			  Long getgId = json2Obj.getgId();
				        			  TblUserAuthorize record = new TblUserAuthorize();
				        			  record.setgAuthorizeInfo(json2Obj.getgAuthorizeInfo());
				        			  record.setgType(json2Obj.getType());
				        			  record.setgUpdatetime(new Date());
				        			  record.setgVersion(1);
				        			  record.setgAuthorizeName(json2Obj.getgAuthorizeName());
				        			  record.setgCreatetime(new Date());
				        			  record.setgUserId(getgId.intValue());
									int in = tblUserAuthorizeMapper.insertSelective(record);
									  if(in==1){
										  if(json2Obj.getShareId()!=null || json2Obj.getShareId()!=0){
							    				TblIntegralSet integralSet = tblIntegralSetMapper.selectByType(6);
							    				TblUser selectByPrimaryKey = tblUserMapper.selectByPrimaryKey((long)json2Obj.getShareId());
							    				TblUser record2 = new TblUser();
							    				record2.setgUserIntegral(selectByPrimaryKey.getgUserIntegral()+integralSet.getIntegral());
							    				record2.setgId((long)json2Obj.getShareId());
							    				record2.setgUserUpdateTime(new Date());
							    				int u= tblUserMapper.updateByPrimaryKeySelective(record2);	
							    				//新增积分明细
							    				TblUserPurseInfo purseInfo = new TblUserPurseInfo();
							    				purseInfo.setgContent("分享链接注册赠送积分");
							    				purseInfo.setgCreatetime(new Date());
							    				purseInfo.setgStatus(1);
							    				purseInfo.setgTitle("分享链接注册");
							    				purseInfo.setgType(2);
							    				purseInfo.setgBalance(selectByPrimaryKey.getgUserIntegral()+integralSet.getIntegral());
							    				purseInfo.setgUserid((long)json2Obj.getShareId());
							    				purseInfo.setgMoney((double)integralSet.getIntegral());
							    				tblUserPurseInfoMapper.insertSelective(purseInfo);
							    				
							    				int i1 = new Double(purseInfo.getgBalance()).intValue();
							    				TblMessage record3 = new TblMessage();
							        			record3.setgContent("分享链接注册获得"+integralSet.getIntegral()+"积分,现有积分"+i1);
							        			record3.setgTitle("积分提示");
							        			record3.setgType(2);
							        			record3.setgUserId((long)json2Obj.getShareId());
							        			record3.setgCreatetime(new Date());
							        			record3.setgVersion(1);
							        			tblMessageMapper.insertSelective(record3);
							    			}
										  TblUser selectByPrimaryKeys = tblUserMapper.selectByPrimaryKeys(getgId);
										  List<TblUserAuthorize> list = selectByPrimaryKeys.getAuthorizes();
										  for(TblUserAuthorize authorize:list){
											  if(authorize.getgType()==1){
												  selectByPrimaryKeys.setWeixinName(authorize.getgAuthorizeName());
											  }else if(authorize.getgType()==2){
												  selectByPrimaryKeys.setWeiboName(authorize.getgAuthorizeName());
											  }else if(authorize.getgType()==3){
												  selectByPrimaryKeys.setQqName(authorize.getgAuthorizeName());
											  } 
										  }
										  
										  //建立上下级关系
							    		  if(json2Obj.getSuperiorId()!=null){
							    			  createRelaiton(json2Obj.getSuperiorId(), json2Obj.getgId().intValue());
							    		  }
										  return new ReturnData(selectByPrimaryKeys, 200, "成功");
									  }
				        		  }
			        	  }else{
			        		  phoneUserInfo.setgUserUpdateTime(new Date());
			        		  if(StringTools.isEmpty(phoneUserInfo.getgUserName())){
			        			  phoneUserInfo.setgUserName(json2Obj.getgAuthorizeName());
			        		  }
			        		  if(StringTools.isEmpty(phoneUserInfo.getgUserName())){
			        			  phoneUserInfo.setgUserAvatar("http://120.79.157.154:8899/hqsmFile/HQSMIMG/head_default.png");
			        		  }
			        		  int i = tblUserMapper.updateByPrimaryKeySelective(phoneUserInfo);
			        		  if(i==1){
			        			  Long getgId = phoneUserInfo.getgId();
			        			  TblUserAuthorize record = new TblUserAuthorize();
			        			  record.setgAuthorizeInfo(json2Obj.getgAuthorizeInfo());
			        			  record.setgType(json2Obj.getType());
			        			  record.setgUpdatetime(new Date());
			        			  record.setgVersion(1);
			        			  record.setgAuthorizeName(json2Obj.getgAuthorizeName());
			        			  record.setgCreatetime(new Date());
			        			  record.setgUserId(getgId.intValue());
								  tblUserAuthorizeMapper.insertSelective(record);
			        		  }
			        		  for(TblUserAuthorize authorize:phoneUserInfo.getAuthorizes()){
					    		  if(authorize.getgType()==1){
					    			  phoneUserInfo.setWeixinName(authorize.getgAuthorizeName());
					    		  }else if(authorize.getgType()==2){
					    			  phoneUserInfo.setWeiboName(authorize.getgAuthorizeName());
					    		  }else if(authorize.getgType()==3){
					    			  phoneUserInfo.setQqName(authorize.getgAuthorizeName());
					    		  }
					    	  }
			        		  return new ReturnData(phoneUserInfo,1002, "当前手机号码已被注册啦。");
			        	  }
			        }
		    	}
		  }
		return new ReturnData(2100, "参数列表为空");
	}

	@Override
	public ReturnData isBind(String authorizedInfo, Integer type) {
	      if(authorizedInfo!=null&&type!=null){
	    	  Map<String, Object> map =new HashMap<>();
	    	  map.put("authorizedInfo", authorizedInfo);
	    	  map.put("type", type);
	    	    TblUserAuthorize Info = tblUserAuthorizeMapper.selectAuthorizedInfo(map);
	    	      if(Info!=null){
	    	    	  Integer getgUserId = Info.getgUserId();
	    	    	  TblUser info = tblUserMapper.selectByPrimaryKeys(Long.valueOf(getgUserId));
	    	    	  if(info.getgStatus()==2){
			    		  return new ReturnData(203,"您的账号已被封号，请联系客服!");
			    	  }
			    	  tblUserMapper.updateLoginTime(info.getgId());
			    	  for(TblUserAuthorize authorize:info.getAuthorizes()){
			    		  if(authorize.getgType()==1){
			    			  info.setWeixinName(authorize.getgAuthorizeName());
			    		  }else if(authorize.getgType()==2){
			    			  info.setWeiboName(authorize.getgAuthorizeName());
			    		  }else if(authorize.getgType()==3){
			    			  info.setQqName(authorize.getgAuthorizeName());
			    		  }
			    	  }
	    	    	  return  new ReturnData(info, 200, "登录成功");
	    	      }else{
	    	    	  return  new ReturnData(1200, "请先绑定手机号码");  
	    	      }
	      }       
	      return  new ReturnData(500, "参数列表为空");
	}

	@Override
	public ReturnData levelOneSort(Integer pageSize, Integer pageNumber) {
           		/*PageHelper.startPage(pageNumber, pageSize);*/
           		 List<TblSort> levelOneSort = tblSortMapper.levelOneSort();
           		/* PageInfo<TblSort>  info =new PageInfo<>(levelOneSort);*/
		return new ReturnData(levelOneSort,/* (int)info.getPages(), (int)info.getTotal(),*/ 200, "兴趣列表");
	}

	@Override
	public ReturnData updateUserInfo(String json) {
		 TblUser json2Obj = StringTools.json2Obj(json, TblUser.class);
		   if(json2Obj!=null){
			   json2Obj.setgUserUpdateTime(new Date());
				int u = tblUserMapper.updateByPrimaryKeySelective(json2Obj);
				  if(u==1){
					  TblUser tblUser = tblUserMapper.selectByPrimaryKeys(json2Obj.getgId());
					  return new ReturnData(tblUser,200, "修改成功");
				  }else{
					  return new ReturnData(2001, "修改失败");
				  }
		   }
		   return  new ReturnData( 1001, "参数列表为空");   
	   }

	// 用户信息
	@Override
	public ReturnData userInfo(Long userId) {
	
		if(userId!=null){
			  TblUser info = tblUserMapper.selectByPrimaryKeys(userId);
			  List<TblUserAuthorize> list = info.getAuthorizes();
			  for(TblUserAuthorize authorize:list){
				  if(authorize.getgType()==1){
					  info.setWeixinName(authorize.getgAuthorizeName());
				  }else if(authorize.getgType()==2){
					  info.setWeiboName(authorize.getgAuthorizeName());
				  }else if(authorize.getgType()==3){
					  info.setQqName(authorize.getgAuthorizeName());
				  } 
			  }
			  return new ReturnData(info, 200, "用户信息");
		}
      return new ReturnData(1200, "暂未用户信息");
	}
	
	// 我的余额
	@Override
	public ReturnData getBalance(Long userId) {
		if(userId!=null){
			  TblUser info = tblUserMapper.selectByPrimaryKey(userId);
			  return new ReturnData(info.getgUserMoney(),200,"我的余额");
		}
      return new ReturnData(1200, "暂未用户信息");
	}

	//我的会员
	@Override
	public ReturnData getVIP(Long userId) {
		// TODO Auto-generated method stub
		TblOrder order = tblOrderMapper.getVipInfo(userId);
		TblVipInfo info = new TblVipInfo();
		if(order!=null){
			info = tblVipInfoMapper.selectByPrimaryKey(order.getgGoodsId());
			if(info!=null){
				info.setBuyTime(order.getgPayTime());
				info.setValidityTime(order.getgValidityTime());
				return new ReturnData(info,200,"会员信息");
			}else{
				return new ReturnData(null,200, "无会员");
			}
		}else{
			return new ReturnData(null,200, "无会员");
		}
	}

	@Override
	public ReturnData getBalanceList(Long userId, Integer pageNumber, Integer pageSize) {
		// TODO Auto-generated method stub
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=1000;
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<TblUserPurseInfo> list = tblUserPurseInfoMapper.selectByList(userId);
		Double rollIn = 0.00;
		Double rollOut = 0.00;
		List<TblUserPurseInfo> list2 = tblUserPurseInfoMapper.selectByList(userId);
		for(TblUserPurseInfo info:list2){
			if(info.getgStatus()==1){
				rollIn +=info.getgMoney();
			}else if(info.getgStatus()==2){
				rollOut +=info.getgMoney();
			}
		}
		PageInfo<TblUserPurseInfo> pageInfo = new PageInfo<>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"钱包明细",new Double(String.format("%.2f",rollIn)),new Double(String.format("%.2f",rollOut)));
	}

	@Override
	public ReturnData enjoyLink(Long userId) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("content", "分享链接赠送积分");
		map.put("userId", userId);
		int ret = tblUserPurseInfoMapper.selectIsEnjoy(map);
		if(ret==0){
			TblIntegralSet integralSet = tblIntegralSetMapper.selectByType(6);
			TblUser user = tblUserMapper.selectByPrimaryKey(userId);
			//增加用户积分
			TblUser tuser = new TblUser();
			tuser.setgId(userId);
			tuser.setgUserIntegral(user.getgUserIntegral()+integralSet.getIntegral());
			int flag1 = tblUserMapper.updateByPrimaryKeySelective(tuser);		
			//新增积分明细
			TblUserPurseInfo purseInfo1 = new TblUserPurseInfo();
			purseInfo1.setgContent("分享链接赠送积分");
			purseInfo1.setgCreatetime(new Date());
			purseInfo1.setgStatus(1);
			purseInfo1.setgTitle("分享链接");
			purseInfo1.setgType(2);
			purseInfo1.setgBalance(user.getgUserIntegral()+integralSet.getIntegral());
			purseInfo1.setgUserid(userId);
			purseInfo1.setgMoney((double)integralSet.getIntegral());
			tblUserPurseInfoMapper.insertSelective(purseInfo1);
			
			int i = new Double(purseInfo1.getgBalance()).intValue();
			TblMessage record3 = new TblMessage();
			record3.setgContent("分享链接获得"+integralSet.getIntegral()+"积分,现有积分"+i);
			record3.setgTitle("积分提示");
			record3.setgType(2);
			record3.setgUserId(userId);
			record3.setgCreatetime(new Date());
			record3.setgVersion(1);
			tblMessageMapper.insertSelective(record3);
		}
		return new ReturnData(200,"分享成功");
	}

	@Override
	public ReturnData updatePwd(String json) {
		// TODO Auto-generated method stub
		try{
			Map<String, Object> map = StringTools.jsonToMap(json);
			Long userId = Long.valueOf(map.get("userId").toString());
			String oldPwd = map.get("oldPwd").toString();
			String newPwd = map.get("newPwd").toString();
			String newPwdAgain = map.get("newPwdAgain").toString();
			
			TblUser user = tblUserMapper.selectByPrimaryKey(userId);
			if(StringTools.isEmpty(oldPwd)){
				return new ReturnData(500,"当前密码不能为空");
			}
			if(!user.getgUserPassword().equals(MD5Util.getMD5byBase64(oldPwd))){
				return new ReturnData(500,"当前密码输入有误");
			}
			if(StringTools.isEmpty(newPwd)){
				return new ReturnData(500,"新密码不能为空");
			}
			if(StringTools.isEmpty(newPwdAgain)){
				return new ReturnData(500,"再次输入密码不能为空");
			}
			if(newPwd.length()<6){
				 return new ReturnData(500, "新密码长度不少于6位");
			}
			if(!newPwd.equals(newPwdAgain)){
				return new ReturnData(500,"两次输入密码不一致");
			}
			
			TblUser tblUser = new TblUser();
			tblUser.setgUserPassword(MD5Util.getMD5byBase64(newPwd));
			tblUser.setgId(userId);
			tblUser.setgUserUpdateTime(new Date());
			int rel = tblUserMapper.updateByPrimaryKeySelective(tblUser);
			if(rel>0){
				return new ReturnData(200,"修改账户密码成功");
			}
			return new ReturnData(500,"修改账户密码失败");
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}

	@Override
	public ReturnData setPayPwd(String json) {
		// TODO Auto-generated method stub
		try{
			Map<String, Object> map = StringTools.jsonToMap(json);
			Long userId = Long.valueOf(map.get("userId").toString());
			String payPwd = map.get("payPwd").toString();
			String payPwdAgain = map.get("payPwdAgain").toString();
			
			if(StringTools.isEmpty("payPwd")){
				return new ReturnData(500,"支付密码不能为空");
			}
			if(StringTools.isEmpty("payPwdAgain")){
				return new ReturnData(500,"再次输入支付密码不能为空");
			}
			
			//RSA解密
			String payWord = RSAUtil.privateDecrypt(payPwd, RSAUtil.getPrivateKey(RSAUtil.privateKey));
			String payWordAgain = RSAUtil.privateDecrypt(payPwdAgain, RSAUtil.getPrivateKey(RSAUtil.privateKey));
			
			
			if(payWord.length()<6){
				return new ReturnData(500,"支付密码不能少于6位");
			}
			if(!payWord.equals(payWordAgain)){
				return new ReturnData(500,"两次输入密码不一致");
			}
			
			TblUser user = new TblUser();
			user.setgId(userId);
			user.setgUserPayPassword(MD5Util.getMD5(payWord));
			user.setgUserUpdateTime(new Date());
		    int ret = tblUserMapper.updateByPrimaryKeySelective(user);
		    if(ret>0){
		    	return new ReturnData(200,"设置支付密码成功");
		    }else{
		    	return new ReturnData(500,"设置支付密码失败");
		    }
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}
	
	@Override
	public ReturnData updatePayPwd(String json) {
		// TODO Auto-generated method stub
		try{
			Map<String, Object> map = StringTools.jsonToMap(json);
			Long userId = Long.valueOf(map.get("userId").toString());
			String oldPayPwd = map.get("oldPayPwd").toString();
			String newPayPwd = map.get("newPayPwd").toString();
			String newPayPwdAgain = map.get("newPayPwdAgain").toString();
			
			TblUser user = tblUserMapper.selectByPrimaryKey(userId);
			if(StringTools.isEmpty(oldPayPwd)){
				return new ReturnData(500,"支付密码不能为空");
			}
			if(StringTools.isEmpty(newPayPwd)){
				return new ReturnData(500,"新密码不能为空");
			}
			if(StringTools.isEmpty(newPayPwdAgain)){
				return new ReturnData(500,"再次输入密码不能为空");
			}
			
			//RSA解密
			String oldPayWord = RSAUtil.privateDecrypt(oldPayPwd, RSAUtil.getPrivateKey(RSAUtil.privateKey));
			String newPayWord = RSAUtil.privateDecrypt(newPayPwd, RSAUtil.getPrivateKey(RSAUtil.privateKey));
			String newPayWordAgain = RSAUtil.privateDecrypt(newPayPwdAgain, RSAUtil.getPrivateKey(RSAUtil.privateKey));
			
			if(!user.getgUserPayPassword().equals(MD5Util.getMD5(oldPayWord))){
				return new ReturnData(500,"支付密码输入有误");
			}
	
			if(newPayWord.length()<6){
				 return new ReturnData(500, "新密码长度不少于6位");
			}
			if(!newPayWord.equals(newPayWordAgain)){
				return new ReturnData(500,"两次输入密码不一致");
			}
			
			TblUser tblUser = new TblUser();
			tblUser.setgUserPayPassword(MD5Util.getMD5(newPayWord));
			tblUser.setgId(userId);
			tblUser.setgUserUpdateTime(new Date());
			int rel = tblUserMapper.updateByPrimaryKeySelective(tblUser);
			if(rel>0){
				return new ReturnData(200,"修改支付密码成功");
			}
			return new ReturnData(500,"修改支付密码失败");
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}
	
	@Override
	public ReturnData forgetPayPwd(String json) throws NoSuchAlgorithmException, InvalidKeySpecException {
		@SuppressWarnings("unchecked")
	    Map<String, Object> jsonToMap = StringTools.jsonToMap(json);	 
		String phone = jsonToMap.get("phone").toString().trim();	 
	    String code = jsonToMap.get("code").toString().trim(); 
		String payPwd = jsonToMap.get("payPwd").toString().trim(); 
		String payPwdAgain = jsonToMap.get("payPwdAgain").toString().trim();	
		
		
		//RSA解密
		String payWord = RSAUtil.privateDecrypt(payPwd, RSAUtil.getPrivateKey(RSAUtil.privateKey));
		String payWordAgain = RSAUtil.privateDecrypt(payPwdAgain, RSAUtil.getPrivateKey(RSAUtil.privateKey));
		
		if(payWord.length()<6){
			 return new ReturnData(500, "密码长度不少于6位");
		}
		
		Map<String, Object> map = new HashMap<>();
    	// 2代表 修改密码 忘记密码
    	map.put("type", 2);
    	map.put("phone", phone);
    	// 获取验证码 对象
    	TblCaptcha code2 = tblCaptchaMapper.getCode(map);
	    if(code2!=null){
		  	long currentTimeMillis = System.currentTimeMillis();
          	//  验证码结束时间内
          	Date expireTime = code2.getExpireTime();				   
          	if(currentTimeMillis-expireTime.getTime()>0){
	    		return new ReturnData(500, "验证码已过期!");
	    	}else if(!code.equals(code2.getCode())){
	    		return new ReturnData(500, "验证码不正确!");
	    	}else{
	    		 if(payWord.equals(payWordAgain)){
	    			 TblUser record = new TblUser();
	    			 record.setgUserPhone(phone);
	    			 record.setgUserUpdateTime(new Date());
	    			 try {
						record.setgUserPayPassword(MD5Util.getMD5(payWord));
					} catch (NoSuchAlgorithmException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NullPointerException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 int u = tblUserMapper.updatePayPassWord(record);
					 if(u==1){
						 return new ReturnData(200, "修改支付密码成功");	 
					 }else{
						 return new ReturnData(100, "修改支付密码失败,此账号不存在!");
					 }
	    		 }else{
	    			 return new ReturnData(1001, "两次密码不一致!");
	    		 }
	    	}
	    }
		return new ReturnData(100,"请先发送验证码");
	}

	@Override
	public ReturnData updatePhone(String json) {
		// TODO Auto-generated method stub
		try{
			Map<String, Object> map = StringTools.jsonToMap(json);
			Long userId = Long.valueOf(map.get("userId").toString());
			String phone = map.get("phone").toString().trim();	 
		    String code = map.get("code").toString().trim(); 
			String password = map.get("password").toString().trim(); 
			
			Map<String, Object> map2 = new HashMap<>();
	    	// 2代表 修改密码 忘记密码
	    	map2.put("type", 4);
	    	map2.put("phone", phone);
	    	// 获取验证码 对象
	    	TblCaptcha code2 = tblCaptchaMapper.getCode(map2);
	    	if(code2!=null){
	    		long currentTimeMillis = System.currentTimeMillis();
	          	//  验证码结束时间内
	          	Date expireTime = code2.getExpireTime();				   
	          	if(currentTimeMillis-expireTime.getTime()>0){
		    		return new ReturnData(500, "验证码已过期!");
		    	}else if(!code.equals(code2.getCode())){
		    		return new ReturnData(500, "验证码不正确!");
		    	}else{
		    		TblUser user = tblUserMapper.selectByPrimaryKey(userId);
		    		if(!user.getgUserPassword().equals(MD5Util.getMD5byBase64(password))){
		    			return new ReturnData(500, "密码不正确!");
		    		}
		    		TblUser selectPhone = tblUserMapper.getPhoneUserInfo(map2);
		    		 if(selectPhone!=null){
		    			  return new ReturnData(1001, "当前手机号已被绑定");
		    		 }
		    		TblUser tblUser = new TblUser();
		    		tblUser.setgId(userId);
		    		tblUser.setgUserPhone(phone);
		    		tblUser.setgUserUpdateTime(new Date());
		    		int rel = tblUserMapper.updateByPrimaryKeySelective(tblUser);
		    		if(rel>0){
		    			return new ReturnData(200,"绑定成功");
		    		}else{
		    			return new ReturnData(200,"绑定失败");
		    		}
		    	}
	    	}else{
	    		return new ReturnData(500,"请先发送验证码");
	    	}
		}catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@Override
	public List<TblUser> getUserInfo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return tblUserMapper.getUserInfo(map);
	}

	@Override
	public int updateStatus(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return tblUserMapper.updateStatus(map);
	}

	@Override
	public ReturnData isSetPayPwd(Long gId) {
		// TODO Auto-generated method stub
		TblUser user = tblUserMapper.selectByPrimaryKey(gId);
		if(user!=null){
			if(!StringTools.isEmpty(user.getgUserPayPassword())){
				return new ReturnData(0,200,"设置了支付密码");
			}else{
				return new ReturnData(1,200,"请先设置支付密码");
			}
		}
		return new ReturnData(500,"用户信息有误");
	}

	@Override
	public TblUser getDetail(Long gId) {
		// TODO Auto-generated method stub
		return tblUserMapper.selectByPrimaryKeys(gId);
	}
	
	@Override
	public List<TblUser> getVipUserInfo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return tblUserMapper.getVipUserInfo(map);
	}

	@Override
	public ReturnData bandThird(String json) {
		// TODO Auto-generated method stub
		try{
			TblUser user = StringTools.json2Obj(json, TblUser.class);
	        Map<String, Object> map = new HashMap<String,Object>();
	        map.put("phone", user.getgUserPhone());
	        TblUser tblUser = tblUserMapper.getPhoneUserInfo(map);
	        if(tblUser!=null){
	        	TblUserAuthorize record = new TblUserAuthorize();
	  			record.setgAuthorizeInfo(user.getgAuthorizeInfo());
	  		    record.setgType(user.getType());
	  			record.setgUpdatetime(new Date());
	  		    record.setgVersion(1);
	  			record.setgAuthorizeName(user.getgAuthorizeName());
	  			record.setgCreatetime(new Date());
	  			record.setgUserId(user.getgId().intValue());
	  			int num = tblUserAuthorizeMapper.insertSelective(record);
	  			if(num>0){
	  				return new ReturnData(user.getgAuthorizeName(),200,"绑定成功");
	  			}else{
	  				return new ReturnData(500,"绑定失败");
	  			}
	        }else{
	        	return new ReturnData(500,"该手机号未注册");
	        }
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}
	
	//建立上下级关系
	private void createRelaiton(Integer superiorId,Integer uId){
		TblRelation relation = new TblRelation();
		relation.setgCreateTime(new Date());
		relation.setgJunior(uId);
		relation.setgSuperior(superiorId);
		tblRelationMapper.insertSelective(relation);
	}

	/**
	 * 获取微信登录第三方信息
	 */
	@Override
	public ReturnData getWeixinInfo(String code,Integer type) {
		ReturnData returnData = new ReturnData();
		try {
			Map<String, String> data = new HashMap();
			if(type==1){ //微信
				// 获取access_token
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("appid", WeixinCons.public_app_id);
				params.put("secret", WeixinCons.public_app_secrect);
				params.put("code", code);
				params.put("grant_type", "authorization_code");
				String xml = HttpRequestUtil.sendRequestByGet("https://api.weixin.qq.com/sns/oauth2/access_token", params);
				// 返回的result成功结果取出access_token：
				Map<String, String> map = StringTools.jsonToMap(xml);
				String access_token = map.get("access_token");
				String refresh_token = map.get("refresh_token");
				String openid = map.get("openid");
		
		        String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
		        JsonObject userInfo = null;
		        try {
		            DefaultHttpClient httpClient = new DefaultHttpClient();
		            HttpGet httpGet = new HttpGet(url);
		            HttpResponse httpResponse = httpClient.execute(httpGet);
		            HttpEntity httpEntity = httpResponse.getEntity();
		            String response = EntityUtils.toString(httpEntity, "utf-8");
		            Gson token_gson = new Gson();
		            userInfo = token_gson.fromJson(response, JsonObject.class);
		            data.put("openid", userInfo.get("openid").toString().replaceAll("\"", ""));
		            data.put("nickname", userInfo.get("nickname").toString().replaceAll("\"", ""));
		            data.put("city", userInfo.get("city").toString().replaceAll("\"", ""));
		            data.put("province", userInfo.get("province").toString().replaceAll("\"", ""));
		            data.put("country", userInfo.get("country").toString().replaceAll("\"", ""));
		            data.put("headimgurl", userInfo.get("headimgurl").toString().replaceAll("\"", ""));
		        } catch (Exception ex) {
		        	ex.printStackTrace();
					return new ReturnData(500,"系统错误");
		        }
			}else if(type==2){ //微博
		        String url1="https://api.weibo.com/oauth2/access_token";
		        String parameters="client_id=" +WeixinCons.weibo_app_key+"&client_secret=" +WeixinCons.weibo_app_secrect+
		        	 "&grant_type=authorization_code" +"&redirect_uri=http://www.shituzx.com" +"&code="+code;
		        trustAllHttpsCertificates();
		        URLConnection conn = new URL(url1).openConnection();
		        conn.setDoOutput(true);// 这里是关键，表示我们要向链接里注入的参数
		        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());// 获得连接输出流
		        out.write(parameters);
		        out.flush();
		        out.close();
		        // 到这里已经完成了，开始打印返回的HTML代码
		        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		        String line = null;
		        String access_token = null;
		        String uid = null;
		        while ((line = reader.readLine()) != null){
		        	Map<String, String> map = StringTools.jsonToMap(line);
					access_token = map.get("access_token");
					uid = map.get("uid");
		        }
		        System.out.println(access_token);
				/*// 返回的result成功结果取出access_token：
				Map<String, String> map = StringTools.jsonToMap(result);
				String access_token = map.get("access_token");
				String uid = map.get("uid");*/
				
		        String url = "https://api.weibo.com/2/users/show.json?access_token=" + access_token + "&uid=" + uid;
		        JsonObject userInfo = null;
		        try {
		            DefaultHttpClient httpClient = new DefaultHttpClient();
		            HttpGet httpGet = new HttpGet(url);
		            HttpResponse httpResponse = httpClient.execute(httpGet);
		            HttpEntity httpEntity = httpResponse.getEntity();
		            String response = EntityUtils.toString(httpEntity, "utf-8");
		            Gson token_gson = new Gson();
		            userInfo = token_gson.fromJson(response, JsonObject.class);
		            data.put("openid", uid);
		            data.put("nickname", userInfo.get("screen_name").toString().replaceAll("\"", ""));
		            data.put("city", userInfo.get("city").toString().replaceAll("\"", ""));
		            data.put("province", userInfo.get("province").toString().replaceAll("\"", ""));
		            data.put("country", "");
		            data.put("headimgurl", userInfo.get("profile_image_url").toString().replaceAll("\"", ""));
		        } catch (Exception ex) {
		        	ex.printStackTrace();
					return new ReturnData(500,"系统错误");
		        } 
			}else if(type==3){  //QQ
				// 获取access_token
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("client_id", WeixinCons.qq_app_id);
				params.put("client_secret", WeixinCons.qq_app_key);
				params.put("code", code);
				params.put("grant_type", "authorization_code");
				params.put("redirect_uri", "http://www.shituzx.com");
				String xml = HttpRequestUtil.sendRequestByGet("https://graph.qq.com/oauth2.0/token", params);
				// 返回的result成功结果取出access_token：
				String[] tt = xml.split("&");
				String access_token = tt[0].toString().split("=")[1];
				
				//获取openid
				Map<String, Object> openMap = null;
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("access_token", access_token);
				String jsonopen = HttpRequestUtil.sendRequestByGet("https://graph.qq.com/oauth2.0/me", param);
				// 返回的result成功结果取出openid：
				String aa = jsonopen.substring(10, jsonopen.length()-3);
				openMap = StringTools.jsonToMap(aa);
				String openid = openMap.get("openid").toString();
		
		        String url = "https://graph.qq.com/user/get_user_info?access_token=" + access_token + "&openid=" + openid + "&oauth_consumer_key="+WeixinCons.qq_app_id;
		        JsonObject userInfo = null;
		        try {
		            DefaultHttpClient httpClient = new DefaultHttpClient();
		            HttpGet httpGet = new HttpGet(url);
		            HttpResponse httpResponse = httpClient.execute(httpGet);
		            HttpEntity httpEntity = httpResponse.getEntity();
		            String response = EntityUtils.toString(httpEntity, "utf-8");
		            Gson token_gson = new Gson();
		            userInfo = token_gson.fromJson(response, JsonObject.class);
		            data.put("openid", openid);
		            data.put("nickname", userInfo.get("nickname").toString().replaceAll("\"", ""));
		            data.put("city", userInfo.get("city").toString().replaceAll("\"", ""));
		            data.put("province", userInfo.get("province").toString().replaceAll("\"", ""));
		            data.put("country", "");
		            data.put("headimgurl", userInfo.get("figureurl_1").toString().replaceAll("\"", ""));
		        } catch (Exception ex) {
		        	ex.printStackTrace();
					return new ReturnData(500,"系统错误");
		        }
			}
			Map<String, Object> map2 = new HashMap<>();
	        map2.put("userInfo", data);
	        returnData.setCode(200);
			returnData.setData(map2);
			returnData.setMsg("请求成功");
			return returnData;
	   }catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
	   }
	}
	
	private static void trustAllHttpsCertificates() throws Exception{
	    javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
	    trustAllCerts[0] = new X509TrustManager(){
	        public X509Certificate[] getAcceptedIssuers() {
	              return null;
	        }
	        public void checkServerTrusted(X509Certificate[] arg0, String arg1)
	              throws CertificateException
	        {}
           public void checkClientTrusted(X509Certificate[] arg0, String arg1)
	              throws CertificateException
	        {}
	    };
	    javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
	    sc.init(null, trustAllCerts, null);
	    javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
	}

	/**
	  * 更新用户mac地址
	  */
	@Override
	public ReturnData updateMac(Integer userid, String mac) {
		Map<String, Object> userInfo = tblUserMapper.selectUserInfo(userid);
		if(mac.equals(userInfo.get("G_MAC"))){
			return new ReturnData(200,"成功");
		}else{
			if(userInfo.get("G_MAC") != null){
				AuroraPush.pushTargetForDriver((String)userInfo.get("G_MAC"),"您的账号已在其他设备登录，您已被迫下线！");
			}
			Map<String,Object> map = new HashMap<>();
			map.put("userid", userid);
			map.put("mac", mac);
			int flag = tblUserMapper.updateMac(map);
			return new ReturnData(200,"成功");
		}
	}
}
